package com.zygh.lz.controller;

import com.zygh.lz.entity.Account;
import com.zygh.lz.service.AccountConfigService;
import com.zygh.lz.service.AccountService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.stapprove.SysPcLog;
import com.zygh.lz.stapprove.ViLog;
import com.zygh.lz.util.Md5;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Objects;

@RequestMapping("/account")
@RestController
public class AccountController {
    @Resource
    private AccountService accountService;

    @GetMapping("/sel")
    @ApiOperation("查询账户信息")
    public ResultBean sel(Account account) {
        return accountService.sel(account);
    }

    @PostMapping("/add")
    @ApiOperation("新增账号信息")
    @SysPcLog(module = "新增账号信息", operation = "修改")
    public ResultBean add(@RequestBody Account account) {
        Account accountExist = accountService.selByStaffTel(account.getStaffTel());
        if (Objects.nonNull(accountExist)) {
            return ResultUtil.setError(500, "存在相同的账号", null);
        }
        String staff_password = Md5.MD5Encode(account.getStaffPassword(), "utf-8");
        account.setStaffPassword(staff_password);
        return accountService.add(account);
    }

    @PostMapping("/del")
    @ApiOperation("删除账号信息")
    public ResultBean del(@RequestBody Account account) {
        return accountService.del(account);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    @SysPcLog(module = "删除账号信息", operation = "删除")
    public ResultBean update(@RequestBody Account account) {

        if (Objects.nonNull(account.getStaffPassword())) {
            String staff_password = Md5.MD5Encode(account.getStaffPassword(), "utf-8");
            account.setStaffPassword(staff_password);
        }
        return accountService.update(account);
    }
}
